package com.hc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hc.constant.UserConst;
import com.hc.domain.Cart;
import com.hc.domain.CartItem;
import com.hc.domain.Sku;
import com.hc.domain.User;
import com.hc.mapper.CartMapper;
import com.hc.res.CartDetail;
import com.hc.res.CartDetailItem;
import com.hc.service.CartItemService;
import com.hc.service.CartService;
import com.hc.service.SkuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author hecai
 * @date 2021/12/20 14:46
 */
@Service
public class CartServiceImpl extends ServiceImpl<CartMapper, Cart> implements CartService {

    @Resource
    private SkuService skuService;
    @Resource
    private CartItemService cartItemService;

    @Override
    public boolean updateCart(Integer skuId, Integer amount) {
        //购物车
        User user = UserConst.user;
        //获取指定用户的购物车
        Cart cart = baseMapper.selectOne(new QueryWrapper<Cart>().eq("user_id", user.getId()));
        //如果用户没有购物车，就创建并添加到数据库表中
        if (cart == null) {
            cart = Cart.builder()
                    .userId(user.getId())
                    .build();
            baseMapper.insert(cart);
        }

        // 找到购物车中所有的购物项
        List<CartItem> cartItemList = cartItemService.list(new QueryWrapper<CartItem>().eq("cart_id", cart.getId()));
        //购物项
        Sku sku = skuService.getById(skuId);
        CartItem cartItem = CartItem.builder()
                .skuId(skuId)
                .money(sku.getPrice1())
                .cartId(cart.getId())
                .build();

        boolean flag = true;
        for (CartItem item : cartItemList) {
            //如果购物车中已经存在参数对应的sku
            if (item.getSkuId().equals(skuId)) {
                if (amount.equals(0)) {
                    //将sku删除
                    cartItemService.removeById(item.getId());
                } else {
                    cartItem.setId(item.getId());
                    cartItem.setAmount(amount);
                    cartItem.setMoney(sku.getPrice1().multiply(new BigDecimal(amount)));
                    cartItemService.updateById(cartItem);
                }
                flag = false;
                break;
            }
        }
        //如果购物车中不存在参数对应的sku
        if (flag) {
            cartItem.setAmount(amount);
            cartItem.setMoney(sku.getPrice1().multiply(new BigDecimal(amount)));
            //添加
            cartItemService.save(cartItem);
        }

        // 修改购物车的信息
        cartItemList = cartItemService.list(new QueryWrapper<CartItem>().eq("cart_id", cart.getId()));
        BigDecimal totalMoney = BigDecimal.ZERO;
        Integer totalAmount = 0;
        for (CartItem item : cartItemList) {
            totalMoney = totalMoney.add(item.getMoney());
            totalAmount = totalAmount + item.getAmount();
        }
        cart.setTotalMoney(totalMoney);
        cart.setTotalAmount(totalAmount);
        baseMapper.updateById(cart);
        return true;
    }

    @Override
    public CartDetail getCartByUserId(Integer userId) {
        Cart cart = baseMapper.selectOne(new QueryWrapper<Cart>().eq("user_id", userId));

        List<CartDetailItem> cartDetailItemList = new ArrayList<>();
        List<CartItem> cartItemList = cartItemService.list(new QueryWrapper<CartItem>()
                .eq("cart_id", cart.getId()));
        for (CartItem cartItem : cartItemList) {
            Sku sku = skuService.getById(cartItem.getSkuId());
            CartDetailItem cartDetailItem = CartDetailItem.builder()
                    .id(sku.getId())
                    .amount(cartItem.getAmount())
                    .money(cartItem.getMoney())

                    .name(sku.getName())
                    .sellPoint(sku.getSellPoint())
                    .img(sku.getImg())
                    .price1(sku.getPrice1())
                    .build();
            cartDetailItemList.add(cartDetailItem);
        }
        CartDetail cartDetail = CartDetail.builder()
                .id(cart.getId())
                .totalAmount(cart.getTotalAmount())
                .totalMoney(cart.getTotalMoney())
                .cartDetailItemList(cartDetailItemList)
                .build();
        return cartDetail;
    }
}
